clc;
clearvars;
rng('shuffle')
% rng('default')
TempDataFile='Data\TestAnalizeTempFile_NoFS_NoB.mat';
Fitter={'ML','MAP'};
% KNNMethodes={'KNN'};
FeaturesIndex={ [1:5,8:11,28,29] %Subset1 Just PStay
                [1:5,8:11,28,29,30:39]}; % Subset1+2 All Features Included
KNNMethodes={'\itk-nn (\wp_1)',...
             '\itk-nn (\wp_1_+_2)'};
KNNMethodNum=length(KNNMethodes);
ReplaceChaseFlag=true;
if exist(TempDataFile,'file')
    Yes_NO = questdlg({'Cash File Exist','Replace it?'});
    if strcmp(Yes_NO,'No') || strcmp(Yes_NO,'Cancel')
        ReplaceChaseFlag=false;
    end
end
if ReplaceChaseFlag
K=69;
[Train,Test]=GetNoisyDataFromRepository(true);
PredictedValue=zeros(Test.SNum,Test.SetNum,KNNMethodNum);
for k=1:KNNMethodNum
    KNNRegressor = KNNReg(Train.Features(:,FeaturesIndex{k}),Train.Label,'NumNeighbors',K,'RegresiorMethod','InverseDistance');
    for s=1:Test.SetNum
        display(['Analyze Feature for Set # ',num2str(s),' of ',num2str(Test.SetNum),' .',KNNMethodes{k}])
        PredictedValue(:,s,k)=GetRegValue(KNNRegressor,Test.Features(:,FeaturesIndex{k},s));
    end
end
save(TempDataFile,'Test','PredictedValue')
else
load(TempDataFile)
end
%%
BasePlotFileName='Plots\TestDataAnalize1_NoFS_NoB_Fig';
SavePlots=1;
TextSize=14;
%%
%%
FittngMethodsNum=size(Test.FittedW,2);
FMAE=zeros(FittngMethodsNum,Test.SetNum);
for f=1:FittngMethodsNum
for i=1:Test.SetNum
    Erorrs=Test.FittedW(:,f,i)-Test.Label(:,i);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    FMAE(f,i)=mean(AE);
end
end

BestFittngMethodsNum=size(Test.BestFittedW,2);
BFMAE=zeros(BestFittngMethodsNum,Test.SetNum);
for f=1:BestFittngMethodsNum
for i=1:Test.SetNum
    Erorrs=Test.BestFittedW(:,f,i)-Test.Label(:,i);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    BFMAE(f,i)=mean(AE);
end
end
EMAE=zeros(KNNMethodNum,Test.SetNum);
EDnM=zeros(KNNMethodNum,Test.SetNum);
EUpM=zeros(KNNMethodNum,Test.SetNum);
for k=1:KNNMethodNum
for r=1:Test.SetNum
    Erorrs=PredictedValue(:,r,k)-Test.Label(:,r);
    AE = abs(Erorrs);
    EMAE(k,r)=mean(AE);
    EDnM(k,r)=mean(AE(AE<=EMAE(k,r)));
    EUpM(k,r)=mean(AE(AE>=EMAE(k,r)));
end
end
%%
Ind=1:Test.SetNum;
Ind(3)=[];
figure(9)
plot(Test.SetTick(Ind),EMAE(1,Ind)','b','LineWidth',2);
hold on
plot(Test.SetTick(Ind),EMAE(2,Ind)','r','LineWidth',2);
plot(Test.SetTick(Ind),BFMAE(1,Ind)','b--','LineWidth',2);
plot(Test.SetTick(Ind),BFMAE(2,Ind)','r--','LineWidth',2);
hold off
I=1;
% patch([1:12,12:-1:1],[EDnM(I,:),EUpM(I,end:-1:1)],'r','FaceAlpha',0.3,'LineStyle','none')
xlim([0,0.5])
set(gca,'FontSize',TextSize)
% set(gca,'XTick',1:Test.SetNum)
% set(gca,'XTickLabels',Test.SetTick)
% set(gca,'XTickLabelRotation',90)
xlabel('Lapse rate')
ylabel('Mean Absolute Error')
% title({'Bias Variance Trade off By Median and Mean of Absolute Error','Sorted by Median'})
% set(gcf,'Position',[50,50,1200,700]);
legend([KNNMethodes,Fitter],'location','northwest')
set(gcf,'Position',[50,50,1200,500]);
if SavePlots
FileName=[BasePlotFileName,'9_NoiseEfect'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
